#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "tingfeng"
# Date: 2020/4/29

import yaml


class WriteUserCommand:
    def read_data(self):
        '''

        加载yaml数据
        '''
        with open("D:/pycharm/appium_unittest/config/userconfig.yaml") as fr:
                data = yaml.full_load(fr)
        return data

    def get_value(self,key,port):
        '''

        :param key:
        :param port:
        获取value
        '''
        data = self.read_data()
        value = data[key][port]
        return value

    def write_data(self,i,device,bp,port):
        '''

        :param i:
        :param device:
        :param bp:
        :param port:
        写入数据
        '''
        data = self.join_data(i,device,bp,port)
        with open("D:/pycharm/appium_unittest/config/userconfig.yaml","a") as fr:
            yaml.dump(data,fr)

    def join_data(self,i,device,bp,port):
        data = {
        "user_info_"+str(i):{
        "deviceName":device,
        "bp":bp,
        "port":port
        }
        }
        return data

    def clear_data(self):
        with open("D:/pycharm/appium_unittest/config/userconfig.yaml", "w") as fr:
                fr.truncate()
        fr.close()

    def get_file_lines(self):
        data = self.read_data()
        return len(data)

if __name__ == '__main__':
    write_file = WriteUserCommand()
    print(write_file.get_value('user_info_0','port'))

